package com.hyl.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.hyl.anno.login.Login;
import com.hyl.common.utils.RedisTools;
import com.hyl.pojo.user.User;
import com.hyl.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author HYL
 * @create 2017-06-19 下午3:29
 **/
@RequestMapping
@Controller
public class UserController {
    @Autowired
    private RedisTools redisTools;

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public User login(User user, HttpServletRequest request){
      User user1= userService.findByUserNameAndPassword(user);
        String sessionId = request.getSession().getId();
        if (user1 != null) {
            redisTools.setByTime(sessionId, JSONObject.toJSONString(user1),20L);
            return user1;
        }else {
            User user2 = new User();
            user2.setUsername("用户未登入");
            return user2;
        }

    }

    @Login
    @RequestMapping("/test")
    public String test(){

        return "haha";
    }

    /**
     * 自定义缓存框架测试方法
     * @param user
     * @return
     */
    @Login
    @ResponseBody
    @RequestMapping("/cache")
    public User cache(User user){
        return userService.findByUserNameAndPassword(user);
    }



}
